package com.example.huawei.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.huawei.system.entity.CommentRecord;
import com.example.huawei.system.entity.LikeRecord;
import com.example.huawei.system.entity.SysUser;
import com.example.huawei.system.mapper.CommentRecordMapper;
import com.example.huawei.system.service.CommentRecordService;
import com.example.huawei.system.service.LikeRecordService;
import com.example.huawei.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 15097789205
 * @description 针对表【comment_record】的数据库操作Service实现
 * @createDate 2025-06-27 09:50:04
 */
@Service
public class CommentRecordServiceImpl extends ServiceImpl<CommentRecordMapper, CommentRecord>
        implements CommentRecordService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private LikeRecordService likeRecordService;

    @Override
    public IPage<CommentRecord> queryFirst(Page<CommentRecord> page, CommentRecord commentRecord) {
        IPage<CommentRecord> result = baseMapper.queryFirst(page, commentRecord);

        List<CommentRecord> records = result.getRecords();
        //用户map
        Map<String, SysUser> userMap = sysUserService.list().stream().collect(Collectors.toMap(SysUser::getId, user -> user, (k1, k2) -> k1));

        long now = new Date().getTime();
        for (CommentRecord record : records) {
            SysUser sysUser = userMap.get(record.getUserId());
            record.setAvatar(sysUser.getAvatar());
            record.setUsername(sysUser.getUsername());
            record.setJoinTime(((now - sysUser.getCreateTime().getTime()) / (24 * 60 * 60 * 1000)) + "天");
            record.setLikeFlag(likeRecordService.count(
                    new LambdaQueryWrapper<LikeRecord>()
                            .eq(LikeRecord::getUserId, commentRecord.getUserId())
                            .eq(LikeRecord::getReleaseId, record.getId())
                            .eq(LikeRecord::getLikeType, 1)
                            .eq(LikeRecord::getLikeFlag, 1)) > 0
            );

            //二级评论
            List<CommentRecord> secondRecords = baseMapper.selectSecond(record.getId());
            secondRecords.forEach(item -> {
                SysUser sysUser1 = userMap.get(item.getUserId());
                item.setAvatar(sysUser1.getAvatar());
                item.setUsername(sysUser1.getUsername());
                item.setJoinTime(((now - sysUser1.getCreateTime().getTime()) / (24 * 60 * 60 * 1000)) + "天");
            });
            record.setCommentList(secondRecords);
        }

        return result;
    }

    @Override
    public IPage<CommentRecord> querySecond(Page<CommentRecord> page, String parentId) {
        IPage<CommentRecord> result = baseMapper.querySecond(page, parentId);
        List<CommentRecord> records = result.getRecords();
        //用户map
        Map<String, SysUser> userMap = sysUserService.list().stream().collect(Collectors.toMap(SysUser::getId, user -> user, (k1, k2) -> k1));

        long now = new Date().getTime();
        for (CommentRecord record : records) {
            SysUser sysUser = userMap.get(record.getUserId());
            record.setAvatar(sysUser.getAvatar());
            record.setUsername(sysUser.getUsername());
            record.setJoinTime(((now - sysUser.getCreateTime().getTime()) / (24 * 60 * 60 * 1000)) + "天");
        }

        return result;
    }
}




